//***************************************************
// A Digit's Widget - Zbrush Zscript
// Color Widget
// 3D Color Gradient Functions
//***************************************************
//Document stuff
[VarSet,docWidth,DOCUMENT:MODIFIERS:WIDTH]
[VarSet,docHeight,DOCUMENT:MODIFIERS:HEIGHT]
[VarSet,docMidX,docWidth/2]
[VarSet,docMidY,docHeight/2]
//some of this stuff maybe not needed but just in case
//Widget's Stuff
[varDef,ICurrX,0]
[varDef,ICurrY,0]
[varDef,ICurrZ,0]
[varDef,ICurrXS,0]
[varDef,ICurrYS,0]
[varDef,ICurrZS,0]
[varDef,CurrXR,0]
[varDef,CurrYR,0]
[varDef,CurrZR,0]
//mousecommon
[varDef,Hval1,320]
[varDef,Vval1,258]
[varDef,Hval2,320]
[varDef,Vval2,258]
[varDef,Zval1,0]
[varDef,Zval2,0]
[varDef,Ldiam,10]
//pixol information common
[VarDef,GetColors,0]
[VarDef,GetDepthandMat,1]
[VarDef,GetNorms,2]
[VarDef,CenterColor,0]
[VarDef,CColor,0]
[VarDef,Rcolor,0]
[VarDef,Gcolor,0]
[VarDef,Bcolor,0]
[VarDef,Pmat,0]
[VarDef,Normx,0]
[VarDef,Normy,0]
[VarDef,Normz,0]
[VarDef,dummy,0]
[FontSetSizeSmall][FontSetColor,255,255,255]
[PD]
[FontSetColor,255,200,0]Color Gradient Utility[FontSetColor,160,160,160]
[PD]
//**********************
//ColorWidget Definitions
//**********************
[VarDef,CRadius,50]
[VarDef,Sred(10)]
[VarDef,Sgreen(10)]
[VarDef,Sblue(10)]
[VarDef,rincrm,1]
[VarDef,gincrm,1]
[VarDef,bincrm,1]
[VarDef,colcntr,0]
[VarDef,XYcolorDepth,500]
[VarDef,ZcolorDepth,500]
[VarDef,XcolorCenter,0]
[VarDef,YcolorCenter,0]
[VarDef,ZcolorCenter,0]
[VarDef,cxmode,0]
[VarDef,cymode,0]
[VarDef,czmode,1]
[VarDef,deltr,0]
[VarDef,deltg,0]
[VarDef,deltb,0]
[VarDef,Samps(255)]
[VarDef,Sptr,0]
[VarDef,hDelta,0]
[VarDef,vDelta,0]
[VarDef,hcnt,0]
[VarDef,vcnt,0]
[VarDef,ccolor,0]
[VarDef,chk,0]
[VarDef,found,0]
[VarDef,Sampend,0]
[VarDef,PHval1,0]
[VarDef,PHval2,0]
[VarDef,PVval1,0]
[VarDef,PVval2,0]
[VarDef,PZVal,0]
[VarDef,pensize,0]
[VarDef,vsizer,0]
[VarDef,hsizer,0]
[VarDef,maxZdelta,500]
//***************************************************
// Digit's Widgets - A Zbrush Zscript
// Color Widget Control
//********************************************************
[PD]
[FontSetColor,255,200,0]Color Gradient Utility[FontSetColor,160,160,160]
[PD]
//Must reset everything when something changes
[iSlider,"ColorStep  ",50,1,1,255,"Gradient Resolution",
	[varSet,CRadius,zscript:ColorStep]
	[VarSet,rincrm,(Sred(1)-Sred(0))/Cradius]
	[VarSet,gincrm,(Sgreen(1)-Sgreen(0))/Cradius]
	[VarSet,bincrm,(Sblue(1)-Sblue(0))/Cradius]
	[if,rincrm=0,[VarSet,rincrm,.00001]]
	[if,gincrm=0,[VarSet,gincrm,.00001]]
	[if,bincrm=0,[VarSet,bincrm,.00001]]
	[VarSet,Rcolor,Sred(0)]
	[VarSet,Gcolor,Sgreen(0)]
	[VarSet,Bcolor,Sblue(0)]
,0,[TextCalcWidth,XYAngle1234567890]]
[iSlider,"ColorXYDepth  ",500,1,-4000,4000,"Set Color XYdepth Maximum",[VarSet,xycolorDepth,zscript:ColorXYDepth],0,[TextCalcWidth,XYAngle1234567890]]
[iSlider,"ColorZDepth  ",500,1,-4000,4000,"Set Color Zdepth Maximum",[VarSet,ZcolorDepth,zscript:ColorZDepth],0,[TextCalcWidth,XYAngle1234567890]]
[ibutton,"3DColor","Color Current Object with 3DColor",
	[transformget,kx,ky,kz]			//what Z do we be?
	[RoutineCall,ZcolorCalc,kx,ky,kz]
]
[IButton,"ColorPick ","Pick a color based on XYZ Pixol on Canvas",
	[RoutineCall,Mouselook,Hval1,Vval1,Hval2,Vval2,Clicked]
	[RoutineCall,PixolLook,Hval1,Vval1,1,Zval1,dummy,dummy,dummy]
	[RoutineCall,ZcolorCalc,Hval1,Vval1,Zval1]
]
[PD]
//determine what axis to use, you can use more than one
[iSwitch,"C-X",1,"X calc",]
[iSwitch,"C-Y",1,"Y calc",]
[iSwitch,"C-Z",1,"Z calc",]

[iSlider,"XColorCtr ",0,1,-4000,4000,"Current X Center",[VarSet,XcolorCenter,zscript:XColorCtr],0,[TextCalcWidth,XYAngle1234567890]]
[iSlider,"YColorCtr ",0,1,-4000,4000,"Current Y Center",[VarSet,YcolorCenter,zscript:YColorCtr],0,[TextCalcWidth,XYAngle1234567890]]
[iSlider,"ZColorCtr ",0,1,-4000,4000,"Current Z Center",[VarSet,ZcolorCenter,zscript:ZColorCtr],0,[TextCalcWidth,XYAngle1234567890]]
[ibutton,"Set-Center","Set Center for ZColor",
	[transformget,XcolorCenter,YcolorCenter,ZcolorCenter]			//what Z do we be?
	[Iset,zscript:XcolorCtr,XcolorCenter]
	[Iset,zscript:YcolorCtr,YcolorCenter]
	[Iset,zscript:ZcolorCtr,ZcolorCenter]
]
[IButton,"PickCenter ","Pick the Center based on XYZ Pixol on Canvas",
	[RoutineCall,Mouselook,XcolorCenter,YcolorCenter,Hval2,Vval2,Clicked]
	[RoutineCall,PixolLook,XcolorCenter,YcolorCenter,1,ZcolorCenter,dummy,dummy,dummy]
	[Iset,zscript:XcolorCtr,XcolorCenter]
	[Iset,zscript:YcolorCtr,YcolorCenter]
	[Iset,zscript:ZcolorCtr,ZcolorCenter]
	[Note,"You May Have to Set Z Manually",,1]
]
[PD]
[iSlider,"Red1  ",0,1,1,255,"Selected Start Red",
	[varSet,Sred(0),zscript:Red1]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[iSlider,"Green1",0,1,1,255,"Selected Start Green",
	[varSet,Sgreen(0),zscript:Green1]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[iSlider,"Blue1 ",0,1,1,255,"Selected Start Blue",
	[varSet,Sblue(0),zscript:Blue1]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[ibutton,"StartColor","Pick Starting Color",
	[VarSet,Sred(0),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(0),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(0),[IGet,Color:Bluecomponent]]
	//Init color
	[RoutineCall,initcolor]
	[Iset,zscript:Red1,Sred(0)]
	[Iset,zscript:Green1,Sgreen(0)]
	[Iset,zscript:Blue1,Sblue(0)]
	[Iset,zscript:Red2,Sred(1)]
	[Iset,zscript:Green2,Sgreen(1)]
	[Iset,zscript:Blue2,Sblue(1)]
]
[PD]
[iSlider,"Red2  ",255,1,1,255,"Selected End Red",
	[varSet,Sred(1),zscript:Red2]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[iSlider,"Green2",255,1,1,255,"Selected End Green",
	[varSet,Sgreen(1),zscript:Green2]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[iSlider,"Blue2 ",255,1,1,255,"Selected End Blue",
	[varSet,Sblue(1),zscript:Blue2]
	[RoutineCall,initcolor]
,0,[TextCalcWidth,XYAngle12345]]
[ibutton,"EndColor","Pick Ending Color",
	[VarSet,Sred(1),[IGet,Color:Redcomponent]]
	[RoutineCall,initcolor]
	[Iset,zscript:Red1,Sred(0)]
	[Iset,zscript:Green1,Sgreen(0)]
	[Iset,zscript:Blue1,Sblue(0)]
	[Iset,zscript:Red2,Sred(1)]
	[Iset,zscript:Green2,Sgreen(1)]
	[Iset,zscript:Blue2,Sblue(1)]
]
[ibutton,"Swap","Swap Start and Ending Color",
	[VarSet,dummy,SRed(1)]
	[VarSet,Sred(1),Sred(0)]
	[VarSet,Sred(0),dummy]
	[VarSet,dummy,Sgreen(1)]
	[VarSet,Sgreen(1),Sgreen(0)]
	[VarSet,Sgreen(0),dummy]
	[VarSet,dummy,Sblue(1)]
	[VarSet,Sblue(1),Sblue(0)]
	[VarSet,Sblue(0),dummy]
	[RoutineCall,initcolor]
	[Iset,zscript:Red1,Sred(0)]
	[Iset,zscript:Green1,Sgreen(0)]
	[Iset,zscript:Blue1,Sblue(0)]
	[Iset,zscript:Red2,Sred(1)]
	[Iset,zscript:Green2,Sgreen(1)]
	[Iset,zscript:Blue2,Sblue(1)]
]
//Pallete Save and Load
[PD]
[ibutton,"L1","Look This Color",	[Note,,zscript:L1,1,[RGB,Sred(3),Sgreen(3),Sblue(3)]]
,0,40]
[ibutton,"L2","Look This Color",	[Note,,zscript:L2,1,[RGB,Sred(4),Sgreen(4),Sblue(4)]]
,0,40]
[ibutton,"L3","Look This Color",	[Note,,zscript:L3,1,[RGB,Sred(5),Sgreen(5),Sblue(5)]]
,0,40]
[ibutton,"L4","Look This Color",	[Note,,zscript:L4,1,[RGB,Sred(6),Sgreen(6),Sblue(6)]]
,0,40]
[ibutton,"L5","Look This Color",	[Note,,zscript:L5,1,[RGB,Sred(7),Sgreen(7),Sblue(7)]]
,0,40]
[PD]
[ibutton,"R1","Recall This Color",	
	[IcolorSet,Sred(3),Sgreen(3),Sblue(3)]
,0,40]
[ibutton,"R2","Recall This Color",	[
	IcolorSet,Sred(4),Sgreen(4),Sblue(4)]
,0,40]
[ibutton,"R3","Recall This Color",	
	[IcolorSet,Sred(5),Sgreen(5),Sblue(5)]
,0,40]
[ibutton,"R4","Recall This Color",	
	[IcolorSet,Sred(6),Sgreen(6),Sblue(6)]
,0,40]
[ibutton,"R5","Recall This Color",	
	[IcolorSet,Sred(7),Sgreen(7),Sblue(7)]
,0,40]
[PD]
[ibutton,"S1","Save Current Color",
	[VarSet,Sred(3),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(3),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(3),[IGet,Color:Bluecomponent]]
	,0,40]
[ibutton,"S2","Save Current Color",
	[VarSet,Sred(4),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(4),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(4),[IGet,Color:Bluecomponent]]
,0,40]
[ibutton,"S3","Save Current Color",
	[VarSet,Sred(5),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(5),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(5),[IGet,Color:Bluecomponent]]
,0,40]
[ibutton,"S4","Save Current Color",
	[VarSet,Sred(6),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(6),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(6),[IGet,Color:Bluecomponent]]
,0,40]
[ibutton,"S5","Save Current Color",
	[VarSet,Sred(7),[IGet,Color:Redcomponent]]
	[VarSet,Sgreen(7),[IGet,Color:Greencomponent]]
	[VarSet,Sblue(7),[IGet,Color:Bluecomponent]]
,0,40]
[PD]
[FontSetColor,255,200,0]Color Stroke Utility[FontSetColor,160,160,160]
[PD]
[iSwitch,"Use Samples",0,"Use Sampled Colors",
	[if,zscript:UseZColor,
		[IToggle,zscript:UseZColor]
	]
	[if,zscript:UseShiftColor,
		[IToggle,zscript:UseShiftColor]
	]
] 
[iSwitch,"Use ZColor",1,"Use Zcolor Function Colors",
	[if,zscript:UseSamples,
		[IToggle,zscript:UseSamples]
	]
	[if,zscript:UseShiftColor,
		[IToggle,zscript:UseShiftColor]
	]
] 
[iSwitch,"Use ShiftColor",0,"Use Shifted Colors",
	[if,zscript:UseZColor,
		[IToggle,zscript:UseZColor]
	]
	[if,zscript:UseSamples,
		[IToggle,zscript:UseSamples]
	]
] 
[iButton,"ShiftAxis","Change RGB Axis for Shifting Color",
	[VarInc,Aptr]
	[if,Aptr=3,[VarSet,Aptr,0]]
	[if,Aptr=0,	[note,"XYZ = RGB",,1]]
	[if,Aptr=1,	[note,"XYZ = GBR",,1]]
	[if,Aptr=2,	[note,"XYZ = BRG",,1]]
]
[iSwitch,"Use SetDepth",0,"Limit Z to value"]
[iSlider,"MxZValue",500,1,0,4000,"Maximum Z Delta ",[VarSet,maxZdelta,zscript:MxZValue],0,[TextCalcWidth,SizeIncr4123]]
[iSlider,"SResolution",0,1,0,128,"Stroke Resolution ",[VarSet,dsizer,zscript:SResolution],0,[TextCalcWidth,SizeIncr4123]]
[iButton," GetBstroke ","Grab the Stroke Data",
	[VarSet,theStroke,[StrokeGetLast]] //get the last drawn stroke
]
[VarDef,cnt,0]
[VarDef,Dsizer,0]
[VarDef,dchk,0]
[VarDef,ddel,0]
[iButton," DoBStrokeColor","Do Stroke With Color",
	[VarSet,numpts,[StrokeGetInfo,theStroke,0]]
	//get xy of origin
	[VarSet,ox,[StrokeGetInfo,theStroke,1,0]]
	[VarSet,oy,[StrokeGetInfo,theStroke,2,0]]
	[VarSet,cnt,0]
	[VarSet,Pixcnt,0]
	[VarSet,sptr,0]
	[VarSet,ddel,0]
	[VarSet,dsizer,[Iget,zscript:Sresolution]]
	[if,zscript:UseSamples,
		[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
		[IColorSet,rcolor,gcolor,bcolor]
		[VarInc,sptr]
	]
	[if,zscript:UseZcolor,
		[RoutineCall,ZcolorCalc,ox,oy,0]
	]
	[if,zscript:UseShiftColor,
		[RoutineCall,PixolLook,ox,oy,1,Zval1,dummy,dummy,dummy]
		[RoutineCall,ShiftColor,ox,oy,Zval1,aptr]
	]
	[CanvasClick,ox,oy,ox,oy]

	[loop,numpts,
		[VarSet,ex,[StrokeGetInfo,theStroke,1,cnt]]
		[VarSet,ey,[StrokeGetInfo,theStroke,2,cnt]]

		[if,Pixcnt>dsizer,
			[VarSet,Pixcnt,0]
			[RoutineCall,PixolLook,ex,ey,1,Zval1,dummy,dummy,dummy]
			[if,zscript:UseSamples,
				[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
				[IColorSet,rcolor,gcolor,bcolor]
				[VarInc,sptr]
				[if,sptr=sampend,[VarSet,sptr,0]]
			]
			[if,zscript:UseZcolor,
				[RoutineCall,ZcolorCalc,ex,ey,Zval1]
			]
			[if,zscript:UseShiftColor,
				[RoutineCall,ShiftColor,ex,ey,Zval1,aptr]
			]
			[if,zscript:UseSetDepth,
				[varSet,dchk,abs(zcolorcenter-zval1)]
				[if,dchk < maxZDelta,
					[CanvasClick,ex,ey,ex,ey]
				,//else
					[VarSet,ddel,1]
				]
			,//else
				[CanvasClick,ex,ey,ex,ey]
			]
			[VarSet,ox,ex]
			[VarSet,oy,ey]
		]
		[VarInc,cnt]
		[VarInc,Pixcnt]
	]
	[if,zscript:UseSetDepth,
		[if,ddel>0,[Note,"Depth Achieved",,1]]
	]
]
[iSlider,"NumSamples",0,1,0,512,"Number of Sampled Colors ",,0,[TextCalcWidth,SizeIncr4123]]
[iButton," GetSamples","Sample Color with Mouse",
	[VarSet,Sptr,0]
	[RoutineCall,Mouselook,Hval1,Vval1,Hval2,Vval2,Clicked]
	[VarSet,hDelta,Hval2-Hval1]
	[VarSet,vDelta,Vval2-Vval1]
	[VarSet,hcnt,Hval1]
	[VarSet,vcnt,Vval1]
	[VarSet,ccolor,0]
	[note,Working,,1]
	
	[loop,vDelta,
		[note,sptr,,1]
		[loop,hDelta,
			[RoutineCall,PixolLook,hcnt,vcnt,0,ccolor,dummy,dummy,dummy]
			[VarSet,chk,0]
			[VarSet,found,0]
			//see if we already have it
			[loop,sptr,
				[if,ccolor=samps(chk),
					[VarSet,found,1]
					[LoopExit]
				]
				[VarInc,chk]
			]
			[if,found=0,
				[VarSet,Samps(sptr),ccolor]
				[VarInc,sptr]
			]
			[if,sptr>=255,[LoopExit]]
			[VarInc,hcnt]
		]
		[if,sptr>=255,[LoopExit]]
		[VarSet,hcnt,Hval1]
		[VarInc,vcnt]
	]
	[VarSet,sampend,sptr]
	[note,Done,,1]
	[ISet,zscript:NumSamples,sptr]
]
[PD]
[VarDef,mskR,0]
[VarDef,mskG,0]
[VarDef,mskB,0]
[VarDef,mskcolor,0]
[VarDef,chkcolor,0]
[iSwitch,"PixolColor",0,"Build up Colors"]
[iSwitch,"Use Mask",0,"Build up only Masked Color"]
[iButton,"Get Mask","Grab Mask Color",
	[VarSet,mskR,[IGet,Color:Redcomponent]]
	[VarSet,mskG,[IGet,Color:Greencomponent]]
	[VarSet,mskB,[IGet,Color:Bluecomponent]]
	[VarSet,mskcolor,[RGB,mskR,mskG,mskB]]
]
[iSwitch,"Use Drag",0,"Use Drag Stroke"]
[iButton," PixolPaint","Color 1 Pixol width with Mouse",
	[VarSet,Sptr,0]
	[RoutineCall,Mouselook,PHval1,PVval1,PHval2,PVval2,Clicked]
	[VarSet,hDelta,PHval2-PHval1]
	[VarSet,vDelta,PVval2-PVval1]
	[VarSet,hcnt,PHval1]
	[VarSet,vcnt,PVval1]
	[VarSet,ccolor,0]
	[VarSet,sptr,0]
	[VarSet,pensize,[Iget,Draw:DrawSize]]
	[VarSet,vsizer,INT(vdelta/pensize)+1]
	[VarSet,hsizer,INT(hdelta/pensize)+1]
	[VarSet,pensize,pensize*2]
	[VarSet,ddel,0]

	[if,zscript:usedrag,
		//Find Center
		[VarSet,vsizer,INT(vdelta/2)]
		[VarSet,hsizer,INT(hdelta/2)]
		[RoutineCall,PixolLook,hsizer,vsizer,1,PZval,dummy,dummy,dummy]
		[if,zscript:PixolColor,
			[RoutineCall,PixolLook,hsizer,vsizer,0,chkcolor,rcolor,gcolor,bcolor]
			[if,zscript:usemask,
				[VarSet,tmp,abs(mskcolor-chkcolor)]
				[if,tmp < 1000,
					[IColorSet,rcolor,gcolor,bcolor]
				]
			,//else
				[IColorSet,rcolor,gcolor,bcolor]
			]
		,//else
			[if,zscript:UseZcolor,
				[RoutineCall,ZcolorCalc,hsizer,vsizer,PZval]
			]
			[if,zscript:UseShiftColor,
				[RoutineCall,Shiftcolor,hsizer,vsizer,PZval,aptr]
			]
			[if,zscript:UseSamples,
				[VarSet,sptr,samped/2]
				[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
				[IColorSet,rcolor,gcolor,bcolor]
			]
			[if,zscript:UseSetDepth,
				[varSet,dchk,abs(zcolorcenter-PZval)]
				[if,dchk > maxZDelta,
					[VarSet,ddel,1]
				]
			]
		]

		[IPress,Stroke:DragRect]
		[CanvasClick,PHval1,PVval1,PHval2,PVval2]
		[IPress,Stroke:Dots]
		[note,Phval1,,1]
	,//else
		[RoutineCall,PixolLook,hcnt,vcnt,1,PZval,dummy,dummy,dummy]

		[loop,vSizer,
			[loop,hSizer,

				[RoutineCall,PixolLook,hcnt,vcnt,1,PZval,dummy,dummy,dummy]
				[if,zscript:PixolColor,
					[RoutineCall,PixolLook,hcnt,vcnt,0,chkcolor,rcolor,gcolor,bcolor]
					[if,zscript:usemask,
						[VarSet,tmp,abs(mskcolor-chkcolor)]
						[if,tmp < 1000,
							[IColorSet,rcolor,gcolor,bcolor]
							[CanvasClick,hcnt,vcnt,hcnt,vcnt]
						]
					,//else
						[IColorSet,rcolor,gcolor,bcolor]
						[CanvasClick,hcnt,vcnt,hcnt,vcnt]
					]
				,//else
					[if,zscript:UseZcolor,
						[RoutineCall,ZcolorCalc,hcnt,vcnt,PZval]
					]
					[if,zscript:UseShiftColor,
						[RoutineCall,Shiftcolor,hcnt,vcnt,PZval,aptr]
					]
					[if,zscript:UseSamples,
						[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
						[IColorSet,rcolor,gcolor,bcolor]
						[VarInc,sptr]
						[if,sptr=sampend,[VarSet,sptr,0]]
					]
					[if,zscript:UseSetDepth,
						[varSet,dchk,abs(zcolorcenter-PZval)]
						[if,dchk > maxZDelta,
							[VarSet,ddel,1]
							[LoopExit]
						]
					]

					[CanvasClick,hcnt,vcnt,hcnt,vcnt]
				]
				[VarSet,hcnt,hcnt+pensize]
				[if,hcnt>PHVal2,[loopexit]]
				[if,ddel>0,[LoopExit]]
			]
			[VarSet,hcnt,PHval1]
			[VarSet,vcnt,vcnt+pensize]
			[if,vcnt>PVval2,[loopexit]]
		]
	]
	[if,zscript:UseSetDepth,
		[if,ddel<>0,[note,"Depth Achieved",,1]]
	]
]
[iButton,"RedoLast","Redo Last Pixolpaint",
	[VarSet,Sptr,0]
	[VarSet,hDelta,PHval2-PHval1]
	[VarSet,vDelta,PVval2-PVval1]
	[VarSet,hcnt,PHval1]
	[VarSet,vcnt,PVval1]
	[VarSet,ccolor,0]
	[VarSet,sptr,0]
	[VarSet,pensize,[Iget,Draw:DrawSize]]
	[VarSet,vsizer,INT(vdelta/pensize)+1]
	[VarSet,hsizer,INT(hdelta/pensize)+1]
	[VarSet,hsizer,hsizer*2]
	[VarSet,vsizer,vsizer*2]
	[VarSet,ddel,0]

	[if,zscript:usedrag,
		//Find Center
		[VarSet,vsizer,INT(vdelta/2)]
		[VarSet,hsizer,INT(hdelta/2)]
		[RoutineCall,PixolLook,hsizer,vsizer,1,PZval,dummy,dummy,dummy]

		[if,zscript:PixolColor,
			[RoutineCall,PixolLook,hsizer,vsizer,0,chkcolor,rcolor,gcolor,bcolor]
			[if,zscript:usemask,
				[VarSet,tmp,abs(mskcolor-chkcolor)]
				[if,tmp < 1000,
					[IColorSet,rcolor,gcolor,bcolor]
				]
			,//else
				[IColorSet,rcolor,gcolor,bcolor]
			]
		,//else
			[if,zscript:UseZcolor,
				[RoutineCall,ZcolorCalc,hsizer,vsizer,PZval]
			]
			[if,zscript:UseShiftColor,
				[RoutineCall,Shiftcolor,hsizer,vsizer,PZval,aptr]
			]
			[if,zscript:UseSamples,
				[VarSet,sptr,samped/2]
				[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
				[IColorSet,rcolor,gcolor,bcolor]
			]
			[if,zscript:UseSetDepth,
				[varSet,dchk,abs(zcolorcenter-PZval)]
				[if,dchk > maxZDelta,
					[VarSet,ddel,1]
				]
			]
		]

		[IPress,Stroke:DragRect]
		[CanvasClick,PHval1,PVval1,PHval2,PVval2]
		[IPress,Stroke:Dots]
		[note,Phval1,,1]
	,//else
		[RoutineCall,PixolLook,hcnt,vcnt,1,pzval,dummy,dummy,dummy]
	
		[loop,vSizer,
			[loop,hSizer,
				[RoutineCall,PixolLook,hcnt,vcnt,1,pzval,dummy,dummy,dummy]
				[if,zscript:PixolColor,
					[RoutineCall,PixolLook,hcnt,vcnt,0,chkcolor,rcolor,gcolor,bcolor]
					[if,zscript:usemask,
						[VarSet,tmp,abs(mskcolor-chkcolor)]
						[if,tmp < 1000,
							[IColorSet,rcolor,gcolor,bcolor]
							[CanvasClick,hcnt,vcnt,hcnt,vcnt]
						]
					,//else
						[IColorSet,rcolor,gcolor,bcolor]
						[CanvasClick,hcnt,vcnt,hcnt,vcnt]
					]
				,//else
					[if,zscript:UseZcolor,
						[RoutineCall,ZcolorCalc,hcnt,vcnt,pZval]
					]
					[if,zscript:UseShiftColor,
						[RoutineCall,Shiftcolor,hcnt,vcnt,pZval,aptr]
					]
					[if,zscript:UseSamples,
						[RoutineCall,ConvertComp,samps(sptr),rcolor,gcolor,bcolor]
						[IColorSet,rcolor,gcolor,bcolor]
						[VarInc,sptr]
						[if,sptr=sampend,[VarSet,sptr,0]]
					]
					[if,zscript:UseSetDepth,
						[varSet,dchk,abs(zcolorcenter-PZval)]
						[if,dchk > maxZDelta,
							[VarSet,ddel,1]
							[LoopExit]
						]
					]
					[CanvasClick,hcnt,vcnt,hcnt,vcnt]
				]
				[VarSet,hcnt,hcnt+pensize]
				[if,hcnt>phval2,[loopexit]]
				[if,ddel>0,[LoopExit]]
			]
			[VarSet,hcnt,pHval1]
			[VarSet,vcnt,vcnt+pensize]
			[if,vcnt>pvval2,[loopexit]]
		]
	]
	[if,zscript:UseSetDepth,
		[if,ddel<>0,[note,"Depth Achieved",,1]]
	]
]

[PD]
//*********************************************
//Subroutines
//*********************************************
//=INT((D2-(A4*65536))/256)
//=D2-(A4*65536+B4*256)
//*************************
//ConvertComp - convert composite color to RGB
//*************************
[RoutineDef,ConvertComp,
	[VarSet,ired,INT(icomp/65536)]
	[VarSet,igreen,INT((icomp - (ired*65536))/256)]
	[VarSet,iblue,icomp - ((ired*65536)+(igreen*256))]
//parameters
,icomp,ired,igreen,iblue
]
//***********************
//ZcolorCalc
//Pick a color in the StartEnd gradient based on Z depth
//***********************
[RoutineDef,ZcolorCalc,
	[VarDef,tmp,0]
	[VarDef,tmp1,0]
	[VarDef,r,0]
	[VarDef,g,0]
	[VarDef,b,0]
	[VarDef,r1,0]
	[VarDef,g1,0]
	[VarDef,b1,0]
	[VarDef,r2,0]
	[VarDef,g2,0]
	[VarDef,b2,0]
	[VarDef,r3,0]
	[VarDef,g3,0]
	[VarDef,b3,0]
	[VarDef,totr,0]
	[VarDef,totg,0]
	[VarDef,totb,0]
	[VarSet,totr,0]
	[VarSet,totg,0]
	[VarSet,totb,0]
	[varSet,r,0]
	[VarSet,g,0]
	[VarSet,b,0]
	[varSet,r1,0]
	[VarSet,g1,0]
	[VarSet,b1,0]
	[varSet,r2,0]
	[VarSet,g2,0]
	[VarSet,b2,0]
	[VarSet,totr,0]
	[VarSet,totg,0]
	[VarSet,totb,0]
	[VarSet,tmp,0]
	[VarSet,tmp1,0]

	[if,deltr = 0,[VarSet,deltr,.0001]]
	[if,deltg = 0,[VarSet,deltg,.0001]]
	[if,deltb = 0,[VarSet,deltb,.0001]]
	//X Color
	[if,[IGet,zscript:C-X],
		
		//offset from center
		[VarSet,tmp1,XColorCenter - abs(daX)]
		[VarSet,tmp1,abs(tmp1)]
		//percent of total area
		[VarSet,tmp,(tmp1*100)/ZcolorDepth]
		[VarSet,tmp,tmp/100]
		
		//how many increments of total steps do they be?
		[VarSet,tmp1,CRadius*tmp]
		[VarSet,r1,tmp1*rincrm]
		[VarSet,g1,tmp1*gincrm]
		[VarSet,b1,tmp1*bincrm]
		
	]

	//Y color
	[if,[IGet,zscript:C-Y],
		//offset from center
		[VarSet,tmp1,YColorCenter - abs(daY)]
		[VarSet,tmp1,abs(tmp1)]

		//percent of total area
		[VarSet,tmp,(tmp1*100)/ZcolorDepth]
		[VarSet,tmp,tmp/100]

		//how many increments of total steps do they be?
		[VarSet,tmp1,CRadius*tmp]

		[VarSet,r2,tmp1*rincrm]
		[VarSet,g2,tmp1*gincrm]
		[VarSet,b2,tmp1*bincrm]
	]

	//Z color
	[if,[IGet,zscript:C-Z],
		
		//offset from center
		[VarSet,tmp1,ZcolorCenter - abs(daZ)]
		[VarSet,tmp1,abs(tmp1)]

		//percent of total area
		[VarSet,tmp,(tmp1*100)/ZcolorDepth]
		[VarSet,tmp,tmp/100]
		
		//how many increments of total steps do they be?
		[VarSet,tmp1,CRadius*tmp]
		
		[VarSet,r,tmp1*rincrm]
		[VarSet,g,tmp1*gincrm]
		[VarSet,b,tmp1*bincrm]
	]
	
	//add up increments
	//RED
	[VarSet,totr,r+r1+r2]
	[VarSet,tmp,totr/deltr]		//divide by Red Delta
	[VarSet,tmp1,tmp - INT(tmp)]	//Strip any whole
	[VarSet,tmp,tmp1*deltr]		//Circle thru span
	[VarSet,r,Sred(0)+tmp]		//Add to Start Color

	//GREEN
	[VarSet,totg,g+g1+g2]
	[VarSet,tmp,totg/deltg]
	[VarSet,tmp1,tmp - INT(tmp)]	//Strip any whole
	[VarSet,tmp,tmp1*deltg]
	[VarSet,g,Sgreen(0)+tmp]

	//BLUE
	[VarSet,totb,b+b1+b2]
	[VarSet,tmp,totb/deltb]
	[VarSet,tmp1,tmp - INT(tmp)]	//Strip any whole
	[VarSet,tmp,tmp1*deltb]
	[VarSet,b,Sblue(0)+tmp]



	[if,totr < 0,
		[if, r<Sred(0),
			[VarSet,r,Sred(1) + r]
		]
	,//else rincrm is positive
		[if,r>Sred(1),
			[VarSet,r,Sred(0) + (r-Sred(1))]
		]
		[if,r >= 255,
			[VarSet,r,Sred(0)]
		]
	]

	[if,totg < 0,
		[if, g<Sgreen(0),
			[VarSet,g,Sgreen(1) + g]
		]
	,//else gincrm is positive
		[if,g>Sgreen(1),
			[VarSet,g,Sgreen(0) + (g-Sgreen(1))]
		]
		[if,g >= 255,
			[VarSet,g,Sgreen(0)]
		]
	]

	[if,totb < 0,
		[if, b<Sblue(0),
			[VarSet,b,Sblue(1) + b]
		]
	,//else bincrm is positive
		[if,b>Sblue(1),
			[VarSet,b,Sblue(0) + (b-Sblue(1))]
		]
		[if,b >= 255,
			[VarSet,b,Sblue(0)]
		]
	]

	[IColorSet,r,g,b]
	[transformset,dax,day,daz+1]
	[transformset,dax,day,daz]
,daX,daY,daZ
]

//***********************
//ShiftColor
//Shift RGB Value from Start Color based on XYZ axis
//***********************
[RoutineDef,ShiftColor,
[VarDef,ax,0]
[VarDef,ay,0]
[VarDef,az,0]
[VarDef,stmp1,0]
[VarDef,stmp,0]
[VarDef,chk,0]
[VarDef,srcol,0]
[VarDef,sgcol,0]
[VarDef,sbcol,0]
	[VarSet,chk,0]
	[VarSet,stmp1,0]
	[VarSet,stmp,0]
	[VarSet,srcol,0]
	[VarSet,sgcol,0]
	[VarSet,sbcol,0]
	[if,iaptr=0,
		[VarSet,ax,iex]
		[VarSet,ay,iey]
		[VarSet,az,iez]
		[VarSet,chk,1]
	,//else
		[if,iaptr=1,
			[VarSet,ax,iey]
			[VarSet,ay,iez]
			[VarSet,az,iex]
			[VarSet,chk,2]
		,//else
			[VarSet,ax,iez]
			[VarSet,ay,iex]
			[VarSet,az,iey]
			[VarSet,chk,3]
		]
	]
	[if,chk=0,[Note,"Chk is zero",,1]]
	//RED
	//offset from center
	[if,[Iget,zscript:C-X],
		[VarSet,stmp1,XColorCenter - abs(ax)]
		[VarSet,stmp1,abs(stmp1)]
		//percent of total area
		[VarSet,stmp,(stmp1*100)/ZcolorDepth]
		[if,stmp = 0,
			[VarSet,srcol,Sred(0)]
		,				
			[VarSet,stmp,stmp/100]
			[If,stmp > 1,
				[VarSet,stmp,stmp - int(stmp)]
			]
			//how many increments of total steps do they be?
			[VarSet,stmp1,255*stmp]			//increment
			[VarSet,srcol,Int(srcol + stmp1)]		//new value
			[if,srcol > 255,
				[VarSet,srcol,srcol - 256]
			]
			[if,srcol < 0,
				[VarSet,srcol,srcol + 256]
			]
		]
		,//else
			[VarSet,srcol,Sred(0)]
		]
		//Green
		//offset from center
		[if,[Iget,zscript:C-Y],
			[VarSet,stmp1,YColorCenter - abs(ay)]
			[VarSet,stmp1,abs(stmp1)]
			//percent of total area
			[VarSet,stmp,(stmp1*100)/ZcolorDepth]
			[if,stmp = 0,
				[VarSet,sgcol,Sgreen(0)]
			,
				[VarSet,stmp,stmp/100]
				[If,stmp > 1,
					[VarSet,stmp,stmp - int(stmp)]
				]
				//how many increments of total steps do they be?
				[VarSet,stmp1,255*stmp]			//increment
				[VarSet,sgcol,INT(sgcol + stmp1)]		//new value
				[if,sgcol > 255,
					[VarSet,sgcol,sgcol - 256]
				]
				[if,sgcol < 0,
					[VarSet,sgcol,sgcol + 256]
				]
			]
		,//else
			[VarSet,sgcol,Sgreen(0)]
		]

		//Blue
		//offset from center
		[if,[Iget,zscript:C-Z],
			[VarSet,stmp1,YColorCenter - abs(az)]
			[VarSet,stmp1,abs(stmp1)]
			//percent of total area
			[VarSet,stmp,(stmp1*100)/ZcolorDepth]
			[if,stmp = 0,
				[VarSet,sbcol,Sblue(0)]
			,
				[VarSet,stmp,stmp/100]
				[If,stmp > 1,
					[VarSet,stmp,stmp - int(stmp)]
				]
				//how many increments of total steps do they be?
				[VarSet,stmp1,255*stmp]			//increment
				[VarSet,sbcol,INT(sbcol + stmp1)]		//new value
				[if,sbcol > 255,
					[VarSet,sbcol,sbcol-256]
				]
				[if,sbcol < 0,
					[VarSet,sbcol,sbcol+256]
				]
			]
		,//else
			[VarSet,sbcol,Sblue(0)]
		]
		[if,srcol > 255,
			[note,"red over 255",,1]
			[note,srcol,,1]
			[VarSet,srcol,srcol-256]
		]
		[if,srcol < 0,
			[note,"red less than 0",,1]
			[note,srcol,,1]
		]
		[if,sgcol > 255,
			[note,"green over 255",,1]
			[note,sgcol,,1]
			[VarSet,sgcol,sgcol-256]
		]
		[if,sgcol < 0,
			[note,"green less than 0",,1]
			[note,sgcol,,1]
		]
		[if,sbcol > 255,
			[note,"blue over 255",,1]
			[note,sbcol,,1]
			[VarSet,sbcol,sbcol-256]
		]
		[if,sbcol < 0,
			[note,"blue less than 0",,1]
			[note,sbcol,,1]
		]
		[Icolorset,srcol,sgcol,sbcol]
,iex,iey,iez,iaptr
]
//*************************
// Init Color Variables when changes are made
//*************************
[RoutineDef,initcolor,
	//get delta
	[Varset,deltr,sred(1)-sred(0)]
	[VarSet,deltg,sgreen(1)-sgreen(0)]
	[VarSet,deltb,sblue(1)-sblue(0)]
	[RoutineCall,FindMax,deltr,deltg,deltb,Cradius]
	//auto set colorstep
	[Iset,zscript:ColorStep,Cradius]	
	[VarSet,rincrm,(Sred(1)-Sred(0))/Cradius]
	[VarSet,gincrm,(Sgreen(1)-Sgreen(0))/Cradius]
	[VarSet,bincrm,(Sblue(1)-Sblue(0))/Cradius]
	[if,rincrm=0,[VarSet,rincrm,.00001]]
	[if,gincrm=0,[VarSet,gincrm,.00001]]
	[if,bincrm=0,[VarSet,bincrm,.00001]]
	[VarSet,Rcolor,Sred(0)]
	[VarSet,Gcolor,Sgreen(0)]
	[VarSet,Bcolor,Sblue(0)]
	//get global color for other routines
//	[RoutineCall,ConvertComp,Centercolor,Sred(0),Sgreen(0),Sblue(0)]
	[VarSet,CenterColor,[RGB,Sred(0),Sgreen(0),Sblue(0)]]
]

// Subroutine GetDistance
// Calculate 2D Distance between 2 Points
//*********************************************
[VarDef,DistToMark,0]
[VarDef,DDistToMark,0]
[RoutineDef,GetDistance,
[VarDef,Xtmp,0]
[VarDef,Ytmp,0]
[VarDef,Xresult,0]
[VarDef,Yresult,0]
[VarDef,tmp,0]
	[VarSet,Xtmp,Ox1-Ex1]
	[VarSet,Ytmp,Oy1-Ey1]
	[VarSet,Xresult,Xtmp*Xtmp]
	[VarSet,Yresult,Ytmp*Ytmp]
	[VarSet,DistToMark,SQRT(Xresult+Yresult)]

//list of inputs
,Ox1,Ex1,Oy1,Ey1
]

//******************************
//Mouselook
// Routine to return Mouse Position values
//******************************
[RoutineDef,Mouselook,
	[loop,999999, // "endless" loop, user keeps clicking

		[loop,999999, // wait for mouse click
			[if,[mouseLbutton] = 1,
				[varSet,IHval1,[mouseHpos]]
				[varSet,IVval1,[mouseVpos]]
				[loopexit]
			] // end if
		] // end wait for mouse click loop

		[loop,999999, // now wait for mouse up
			[if,[mouseLbutton] = 0,
				[varSet,IHval2,[mouseHpos]]
				[varSet,IVval2,[mouseVpos]]
				[loopexit]
			] // end if
		] // end wait for mouse up loop

		[if,(IVval1 > 0), //(on the canvas)
			[if,(abs(IHval2-IHval1)<3) && (abs(IVval2-IVval1)<3),
				// single click
				[VarSet,IClicked,0]
			, // else = click + drag
				[VarSet,IClicked,1]
			] // end if click or drag
			[loopExit]
		] // end if (top button clicked/not clicked)
		[loop,99999, // ensure mouse button is up before continuing
			[if,[mouseLbutton] = 0,
				[loopExit]
			] // end if
		] // end ensure loop
	] // end "endless" loop
//parameters
,IHval1,IVval1,IHval2,IVval2,IClicked
]//end Mouselook
//******************************
//Pixollook
// Routine to return All Pixol values
//******************************
[RoutineDef,PixolLook,
	[if,itype = 0,	//return color information
		[VarSet,val1,[PixolPick,0,ihval,ivval]]
		[VarSet,val2,[PixolPick,2,ihval,ivval]]
		[VarSet,val3,[PixolPick,3,ihval,ivval]]
		[VarSet,val4,[PixolPick,4,ihval,ivval]]
	,//else
		[if,itype = 1,	//Return Zdepth and material
			[VarSet,val1,[PixolPick,1,ihval,ivval]]
			[VarSet,val2,[PixolPick,5,ihval,ivval]]
		,//else
			[if,itype = 2,	//Return Normals
				[VarSet,val1,[PixolPick,6,ihval,ivval]]
				[VarSet,val2,[PixolPick,7,ihval,ivval]]
				[VarSet,val3,[PixolPick,8,ihval,ivval]]
			]
		]
	]
//Parameters here
,ihval,ivval,itype,val1,val2,val3,val4
]
[PD]
[pd][penMove,0,8]
<zscriptinsert,"Widgets_Index_Include_Std.txt">
[pd][penMove,0,8]
\C00e0e0About This ZScript:\Cc0c0c0
This script will allow you to paint gradient colors and to use sampled colors.
[PD]
[fontSetSizeSmall]Standard Digit Widget Manual Revision 1.0, created by Digits
